package org.example.springboot.common.generater;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.example.springboot.common.domain.BaseEntity;
import org.example.springboot.common.generater.config.CodeGeneratorProperties;
import org.example.springboot.common.generater.config.JdbcProperties;
import org.example.springboot.common.service.IBaseService;
import org.example.springboot.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Component
public class CodeGenerator {

    public void generator(JdbcProperties jdbcProperties, CodeGeneratorProperties codeGenProperties) {

        String packagePath = "java/" + codeGenProperties.getRootPackage().replaceAll("\\.", "/");
        String outputDir = codeGenProperties.getOutputDir();

        if (StringUtils.hasText(outputDir)) {
            outputDir = outputDir.replace("\\", "/");
            if (!outputDir.endsWith("/")) {
                outputDir += "/";
            }
        }

        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.xml, outputDir + "/resources/mapper");
        pathInfo.put(OutputFile.controller, outputDir + packagePath + "/controller");
        pathInfo.put(OutputFile.entity, outputDir + packagePath + "/domain");
        pathInfo.put(OutputFile.mapper, outputDir + packagePath + "/mapper");
        pathInfo.put(OutputFile.serviceImpl, outputDir + packagePath + "/service/impl");
        pathInfo.put(OutputFile.service, outputDir + packagePath + "/service");


        FastAutoGenerator.create(jdbcProperties.getUrl(), jdbcProperties.getUsername(), jdbcProperties.getPassword())
                .globalConfig(builder -> {
                    builder.author(codeGenProperties.getAuthor()) // 设置作者
                            .outputDir(codeGenProperties.getOutputDir()) // 输出目录
                            .commentDate("yyyy-MM-dd")
                            .dateType(DateType.TIME_PACK) // java8 新的时间类型
                            .disableOpenDir()
                            .enableSwagger(); // 开启 swagger 模式
                })
                .packageConfig(builder -> {
                    builder.parent(codeGenProperties.getRootPackage()) // 设置父包名
                            .moduleName("system") // 设置父包模块名
                            .pathInfo(pathInfo) // 设置生成路径
                            .controller("controller")
                            .mapper("mapper")
                            .serviceImpl("service.impl")
                            .service("service")
                            .entity("domain");
                })
                .strategyConfig(builder -> builder.mapperBuilder()
                        .enableMapperAnnotation()
                        .enableFileOverride()
                        .enableBaseResultMap()
                        .enableBaseColumnList())
                .strategyConfig(builder -> builder.entityBuilder()
                        .enableChainModel()
                        .enableFileOverride()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .enableColumnConstant()
                        .superClass(BaseEntity.class)
                        .addIgnoreColumns("create_time", "creator", "update_time", "updater", "deleted")
                        .addTableFills(
                                new Column("create_time", FieldFill.INSERT),
                                new Column("creator", FieldFill.INSERT),
                                new Column("update_time", FieldFill.INSERT_UPDATE),
                                new Column("updater", FieldFill.INSERT_UPDATE),
                                new Column("deleted", FieldFill.INSERT)
                        )
                        .logicDeleteColumnName("deleted")
                        //.versionColumnName("version")
                )
                .strategyConfig(builder -> builder.serviceBuilder()
                        .superServiceClass(IBaseService.class)
                        .superServiceImplClass(BaseServiceImpl.class)
                )
                .strategyConfig(builder -> {
                    builder
                            .addInclude(codeGenProperties.getTables())
                    ; // 设置需要生成的表名

                    //.addTablePrefix("pigg_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
